package cn.tedu.service.stock.impl;

import cn.tedu.commons.response.JsonResult;
import cn.tedu.mapper.petdetails.PetDetailsMapper;
import cn.tedu.mapper.stock.stockadmin.StockMapper;
import cn.tedu.pojo.dto.order.OrderTebleDTO;
import cn.tedu.pojo.entity.OrderTeble;
import cn.tedu.pojo.pet.entity.PetDetails;
import cn.tedu.pojo.stock.dto.StockDTO;
import cn.tedu.pojo.stock.entity.StockAdmin;
import cn.tedu.service.stock.IStockService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class StockServiceImpl implements IStockService {

    @Autowired(required = false)
    private StockMapper stockMapper;

    @Autowired(required = false)
    private PetDetailsMapper petDetailsMapper;

    @Override
    public void updateStock(StockDTO stockDTO) {

        System.out.println(stockDTO);
        //向库存管理记录保存数据
        StockAdmin stockAdmin = new StockAdmin();
        System.out.println(stockDTO.getCommodityId());
        System.out.println(stockDTO.getStockAddNum());
        stockAdmin.setCommodityId(stockDTO.getCommodityId());
        stockAdmin.setStockAddNum(stockDTO.getStockAddNum());
        stockAdmin.setGmtCreate(LocalDateTime.now());
        stockAdmin.setGmtModified(LocalDateTime.now());
        stockAdmin.setIsDelete(1);
        stockMapper.addInStock(stockAdmin);


        System.out.println(stockDTO.getCommodityId());
        //从商品详情信息种根据提供的商品id提取库存数量并和DTO传来的库存数据进行计算(均要确保是启用中状态)
        PetDetails stockNum = petDetailsMapper.getStockByCommodityId(stockDTO.getCommodityId());
        System.out.println(stockNum.getStock());
        Long newStockNum = stockNum.getStock() + stockDTO.getStockAddNum();
        //向商品详情的库存中修改库存信息
        stockMapper.updateStockToCommodity(newStockNum, LocalDateTime.now(), stockDTO.getCommodityId());

        //打桩完成
        System.out.println("修改完成");

    }

    @Override
    public JsonResult updateStockByOrder(OrderTebleDTO orderTebleDTO) {
        Integer newStockNum = orderTebleDTO.getCommodityNumber();
        Long commodityId = orderTebleDTO.getCommdityId();
        System.out.println("获取的commodityId是：" + commodityId);
        //根据商品id获取商品详情中的库存剩余
        System.out.println(orderTebleDTO);
        PetDetails oldStock = stockMapper.getStockByCommodityId(commodityId);
        System.out.println(oldStock.getStock());
        //根据返回来的值进行库存计算，如果不够则报错
        int a = newStockNum;
        int b =  oldStock.getStock().intValue();
        int stock = b - a;
        System.out.println(a - b);
        System.out.println("要减少的订单数为：" + newStockNum + "库存剩余为：" + oldStock.getStock() + "最终计算得：" + stock);
        if (stock < 0) {
            System.out.println("库存不足！！");
            throw new RuntimeException();
        }
        //把通过计算后的库存写入商品详情中
        stockMapper.updateStockFromOrder(commodityId, stock);
        System.out.println("要修改的商品id为：" + commodityId + "最终库存量为：" + stock);
        return JsonResult.ok();
    }
}
